package com.kyzh.core.pager.weal.quanzi

import android.content.Context
import android.content.Intent
import android.graphics.Color
import android.os.Bundle
import android.view.LayoutInflater
import android.widget.TextView
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseDataBindingHolder
import com.gushenge.core.base.activity.BaseActivity
import com.gushenge.core.beans.DynamicBean
import com.gushenge.core.requests.QuanZiRequest
import com.kyzh.core.R
import com.kyzh.core.adapters.DynamicAdapter
import com.kyzh.core.databinding.ItemDynamicDiscuss2Binding
import com.kyzh.core.databinding.ItemDynamicTopBinding
import com.kyzh.core.dialog.showSendMessageDialog
import com.kyzh.core.utils.reviewImages
import com.kyzh.core.utils.setShader
import com.kyzh.core.utils.setVisibility
import com.kyzh.core.utils.startLogin
import com.littlejerk.rvdivider.builder.XGridBuilder
import com.shuyu.gsyvideoplayer.GSYVideoManager
import kotlinx.android.synthetic.main.activity_pinglun_detail.*

class PingLunDetailActivity : BaseActivity()  {
    companion object{
        @JvmStatic
        fun start(context: Context,dynamic: DynamicBean) {
            val starter = Intent(context, PingLunDetailActivity::class.java).putExtra("data",dynamic)
            context.startActivity(starter)
        }
    }
    private var id:String = ""
    private var pid:String = ""
    private var ppid:String = ""
    private var dynamic: DynamicBean? =null
    private var list : ArrayList<DynamicBean> = arrayListOf()
    private val adapter: PingLunAdapter = PingLunAdapter(list)
    private var dataBindTop:ItemDynamicTopBinding ? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_pinglun_detail)

        initView()
        initData()
    }

    fun initView(){
        swipe.setOnRefreshListener {
            initView()
            initData()
            swipe.isRefreshing = false
        }
        dynamic= intent.getSerializableExtra("data") as DynamicBean
        close.setOnClickListener {
            finish()
        }
        tvDiscuss.setOnClickListener {
            if(startLogin()){
                showSendMessageDialog {
                    QuanZiRequest.sendDynamicPingLun(dongtai_id =dynamic!!.id,content =this){
                        if(this){
                           initData()
                       }
                    }
                }
            }

        }

        initTop()
        rvList1.layoutManager = object : LinearLayoutManager(this, RecyclerView.VERTICAL,false){        }
        rvList1.adapter = adapter
        adapter.removeAllHeaderView()
        adapter.addHeaderView(dataBindTop!!.root)
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if(requestCode == 10000 && resultCode == RESULT_OK){
            initView()
            initData()
        }
    }
    //  加载顶部 view
    fun initTop(){
        dataBindTop = DataBindingUtil.inflate<ItemDynamicTopBinding>(
            layoutInflater,
            R.layout.item_dynamic_top,
            null,
            false
        )
        dataBindTop?.apply {
            QuanZiRequest.getDynamicInfo(dynamic!!.id){
                this?.let {
                    dataBindTop?.data = it
                    tvSubmit.hint = "回复：${it.pet_name}"
                    tvTitle.text = "评论详情"
                    Glide.with(this@PingLunDetailActivity).load(it.face).into(ivHead)
                    tvName.setText(it.pet_name)
                    tvName.setShader(it.color)
                    if (it.img.isNullOrEmpty() && it.video.isNullOrEmpty()) {
                        rvList2.setVisibility(false)
                    }
                    if (it.is_zan == 1) {
                        //点赞了
                        val drawable = resources.getDrawable(R.drawable.ic_zan1)
                        tvDianzan.setCompoundDrawablesWithIntrinsicBounds(
                            drawable,
                            null,
                            null,
                            null
                        )

                    } else {
                        //为点赞
                        val drawable = resources.getDrawable(R.drawable.ic_nozan)
                        tvDianzan.setCompoundDrawablesWithIntrinsicBounds(
                            drawable,
                            null,
                            null,
                            null
                        )
                    }
                    tvDianzan.setOnClickListener { view ->
                        QuanZiRequest.DynamicZan(id) {
                            if (it.is_zan == 1) {
                                val i = it.zan.toInt() - 1
                                it.zan = i
                                it.is_zan = 0
                            } else {
                                val i = it.zan.toInt() + 1
                                it.zan = i
                                it.is_zan = 1

                            }
                            tvDianzan.text = it.zan.toString()
                            if (it.is_zan == 1) {
                                //点赞了
                                val drawable = resources.getDrawable(R.drawable.ic_zan1)
                                tvDianzan.setCompoundDrawablesWithIntrinsicBounds(
                                    drawable,
                                    null,
                                    null,
                                    null
                                )

                            } else {
                                //为点赞
                                val drawable = resources.getDrawable(R.drawable.ic_nozan)
                                tvDianzan.setCompoundDrawablesWithIntrinsicBounds(
                                    drawable,
                                    null,
                                    null,
                                    null
                                )
                            }
                        }

                    }
                    if (!it.video.isNullOrEmpty()) {
                        val bannerLayoutManager =
                            object : GridLayoutManager(this@PingLunDetailActivity, 1) {
                                override fun canScrollHorizontally(): Boolean {
                                    return false
                                }

                                override fun canScrollVertically(): Boolean {
                                    return false
                                }
                            }
                        rvList2.setVisibility(true)
                        var list = arrayListOf<String>()
                        if (it.img == null || it.img.size == 0) {
                            list.add(String())
                        } else {
                            list = it.img
                        }
                        rvList2.layoutManager = bannerLayoutManager
                        val gameDetailBannerAdapter =
                            DynamicAdapter(R.layout.game_detail_banner_item1, list, it.video)
                        rvList2.adapter = gameDetailBannerAdapter

                        gameDetailBannerAdapter.setOnItemClickListener { adapter, view, position ->
                            reviewImages(it.img, position)
                        }
                    } else if (it.img != null && it.img.size > 0 && it.img[0] != "http:" && it.img[0] != "") {

                        val bannerLayoutManager =
                            object : GridLayoutManager(this@PingLunDetailActivity, 6) {
                            }
                        val value = object : GridLayoutManager.SpanSizeLookup() {
                            override fun getSpanSize(position: Int): Int {
                                return when (it.img.size) {
                                    1 -> 6
                                    2 -> 3
                                    else -> 2
                                }
                            }
                        }
                        bannerLayoutManager.spanSizeLookup = value
                        rvList2.setVisibility(true)
                        val verticalIncludeEdge =
                            XGridBuilder(this@PingLunDetailActivity).setSpacing(2f)//这几个Spacing的优先级可看XGridBuilder说明
                                .setVLineSpacing(4f)
                                .setHLineSpacing(4f)
                                //这几个color和drawable的优先级可看XGridBuilder说明
                                .setColor(Color.BLUE)
                                //可设置颜色和drawable,drawable>color
//                .setColorRes(R.color.white)
//                .setDrawable(new ColorDrawable(Color.WHITE))
//                .setDrawableRes(R.drawable.)
                                .setHLineColor(Color.WHITE)
//                .setHLineDrawable()
                                .setVLineColor(Color.WHITE)
//                .setVLineDrawable()
                                //是否包括边界
                                .setIncludeEdge(false)
                                //竖直和水平分割线交叉处绘制的是竖直分割线颜色（交叉处属于竖直分割线）
                                .setVerticalIncludeEdge(true)
                        rvList2.addItemDecoration(verticalIncludeEdge.build())
                        rvList2.layoutManager = bannerLayoutManager
                        val gameDetailBannerAdapter = DynamicAdapter(
                            R.layout.game_detail_banner_item1,
                            it.img,
                            it.video,
                            false
                        )
                        rvList2.adapter = gameDetailBannerAdapter
//                        if (it.img != null && it.img?.size == 1) {
//                            rvList2.setHeight(-2)
//                        } else
//                            if (it.img != null && it.img?.size == 2) {
//                                rvList2.setHeight(dip(115))
//                            } else {
//                                if (it.img?.size!! % 3 == 0) {
//                                    rvList2.setHeight(dip(115) * (it.img?.size!! / 3))
//                                } else {
//                                    rvList2.setHeight(dip(115) * ((it.img?.size!! / 3) + 1))
//                                }
//                            }
                            gameDetailBannerAdapter.setOnItemClickListener { adapter, view, position ->
                            reviewImages(it.img, position)
                        }
                    } else {
                        rvList2.setVisibility(false)
                    }
                }
            }

        }
    }
    fun initData(){
        if (dynamic ==null){
            return
        }else{
            val inflate = LayoutInflater.from(this@PingLunDetailActivity).inflate(R.layout.empty, null)
            val tv1 = inflate.findViewById<TextView>(R.id.tv1)
            tv1.text = "暂无评论"
            id =dynamic!!.id
            QuanZiRequest. getDynamicPingLun(id){
                //
                this?.apply {
                    adapter.setNewInstance(data)
                }
            }
        }
    }

    inner class  PingLunAdapter(data : ArrayList<DynamicBean>) : BaseQuickAdapter<DynamicBean,BaseDataBindingHolder<ItemDynamicDiscuss2Binding>>(R.layout.item_dynamic_discuss2,data){
        override fun convert(
            holder: BaseDataBindingHolder<ItemDynamicDiscuss2Binding>,
            item: DynamicBean
        ) {
            holder.dataBinding?.apply {
                data = item
                rvList.setVisibility(item.data_num != 0)
                tvShow.setVisibility(item.data_num != 0)
                tvShow.text = "查看全部 ${item.data_num} 条回复"
                tvShow.setOnClickListener {
                    item.pinglun = item.data_num.toString()
                    PingLunErJiDetailActivity.start(this@PingLunDetailActivity, item, dynamic!!.id)
                }
                conTitle.setOnClickListener {
                    //  二级评论
                    showSendMessageDialog("回复@${item.pet_name}") {
                        QuanZiRequest.sendDynamicPingLun(dongtai_id =dynamic!!.id,content =this,pid = item.id){
                            if(this){
                                initData()
                            }
                        }
                    }
                }
                if(item.color.isNullOrEmpty()){
                    tvName .setHasColors(false,null)
                    tvName.setText(item.pet_name)
                }else{
                    if(item.color.size == 1){
                        tvName.setText(item.pet_name)
                        tvName .setHasColors(false,null)
                        tvName.setTextColor(Color.parseColor(item.color[0]))
                    }else{
                        var intArray = IntArray(item.color!!.size)
                        item.color?.forEachIndexed { index, s ->
                            intArray[index] = Color.parseColor(s)
                        }
                        tvName.setTextColor(Color.parseColor(item.color[0]))
                        tvName.setText(item.pet_name)
                        tvName .setHasColors(true,intArray)

                    }
                }
                if(item.data!=null){
                    rvList.layoutManager = LinearLayoutManager(this@PingLunDetailActivity)
                    rvList.adapter =Pinglun2Adapter(item.data).apply {
                        //  三级评论
                        setOnItemClickListener { adapter, view, position ->
                            item.pinglun = item.data_num.toString()
                            PingLunErJiDetailActivity.start(
                                this@PingLunDetailActivity,
                                item,
                                dynamic!!.id
                            )
                        }
                    }
                }
                //  点赞
                if(item.is_zan==1){
                    //点赞了
                    val drawable = resources.getDrawable(R.drawable.ic_zan1)
                    tvZan.setCompoundDrawablesWithIntrinsicBounds(null,drawable,null,null)

                }else{
                    //为点赞
                    val drawable = resources.getDrawable(R.drawable.ic_nozan)
                    tvZan.setCompoundDrawablesWithIntrinsicBounds(null,drawable,null,null)
                }
                tvZan.setOnClickListener {
                    QuanZiRequest.DynamicZan(dynamic!!.id,"1"){
                        if(item.is_zan==1){
                            val i = item.zan.toInt() - 1
                            item.zan = i
                            item.is_zan =0
                        }else{
                            val i =item.zan.toInt() + 1
                            item.zan = i
                            item.is_zan =1
                        }
                        tvZan.text = item.zan.toString()
                        if(item.is_zan==1){
                            //点赞了
                            val drawable = resources.getDrawable(R.drawable.ic_zan1)
                            tvZan.setCompoundDrawablesWithIntrinsicBounds(null,drawable,null,null)

                        }else{
                            //为点赞
                            val drawable = resources.getDrawable(R.drawable.ic_nozan)
                            tvZan.setCompoundDrawablesWithIntrinsicBounds(null,drawable,null,null)
                        }
                    }
                }

            }
        }

    }
    // 因为有点击冲突  没想到其他方法 所以必须在写一个
    inner class Pinglun2Adapter(data : ArrayList<DynamicBean>):BaseQuickAdapter<DynamicBean,BaseDataBindingHolder<ItemDynamicDiscuss2Binding>>(R.layout.item_dynamic_discuss2,data){
        override fun convert(
            holder: BaseDataBindingHolder<ItemDynamicDiscuss2Binding>,
            item: DynamicBean
        ) {
            holder.dataBinding?.apply {
                data = item
                rvList.setVisibility(item.data_num != 0)
                tvShow.setVisibility(item.data_num != 0)
                tvShow.text = "查看全部 ${item.data_num} 条回复"
                if(item.color.isNullOrEmpty()){
                    tvName .setHasColors(false,null)
                    tvName.setText(item.pet_name)
                }else{
                    if(item.color.size == 1){
                        tvName.setText(item.pet_name)
                        tvName .setHasColors(false,null)
                        tvName.setTextColor(Color.parseColor(item.color[0]))
                    }else{
                        var intArray = IntArray(item.color!!.size)
                        item.color?.forEachIndexed { index, s ->
                            intArray[index] = Color.parseColor(s)
                        }
                        tvName.setTextColor(Color.parseColor(item.color[0]))
                        tvName.setText(item.pet_name)
                        tvName .setHasColors(true,intArray)

                    }
                }
                if(item.is_zan==1){
                    //点赞了
                    val drawable = resources.getDrawable(R.drawable.ic_zan1)
                    tvZan.setCompoundDrawablesWithIntrinsicBounds(null,drawable,null,null)

                }else{
                    //为点赞
                    val drawable = resources.getDrawable(R.drawable.ic_nozan)
                    tvZan.setCompoundDrawablesWithIntrinsicBounds(null,drawable,null,null)
                }
            }
        }

    }



    override fun onPause() {
        super.onPause()
        GSYVideoManager.onPause()
    }

    override fun onBackPressed() {
        if(GSYVideoManager.isFullState(this)){
            GSYVideoManager.backFromWindowFull(this)
        }else{
            finish()
        }

    }
    override fun onDestroy() {
        super.onDestroy()

    }


}